package engine.logic.walk
{
	import engine.GameData;
	import engine.data.BindManage;
	import engine.data.ModelLocator;
	import engine.flag.GameDefine;
	import engine.net.event.CustomMsgEvent;
	import engine.net.messages.CustomToClientFlag;
	import engine.net.messages.CustomToServerFlag;
	import engine.net.mgr.SystemSender;
	import engine.newUi.MakeSnowLogic;
	import engine.newUi.MakeSnowUI;
	import engine.newUi.MyPropertyLogic;
	import engine.scene.SceneManager;
	import engine.scene.player.BasePlayerModel;
	import engine.scene.player.RoleModel;
	
	import flash.geom.Point;
	import flash.geom.Rectangle;
	
	public class WalkManager
	{
		private static	 var _self:WalkManager;
		
		public static function getInstance():WalkManager
		{
			if ( _self == null )
			{
				_self = new WalkManager();
			}
			return _self;
		}
		
		private var _model 		: ModelLocator;
		
		private var _findWayLogic	: FindWayUtil;
		
		private var _walkLogic		: WalkLogic;
		
		private var _bindManage	: BindManage;
		
		public function WalkManager():void
		{
			init();
		}
		
		private function init():void
		{
			_model			=	ModelLocator.getInstance();
			
			_walkLogic		=	new WalkLogic();
			
			_findWayLogic	=	new FindWayUtil();
			
			_bindManage 	= 	BindManage.getInstance();
			
			_model.addEventListener(String(CustomToClientFlag.SERVER_CUSTOMMSG_PLAYER_MOVETO), 
				onPlayerMove);
			
			_model.addEventListener(String(CustomToClientFlag.SERVER_CUSTOMMSG_PLAYER_STOP), 
				onPlayerStop);
		}
		
		
		//主角移动
		public function roleMove( player : BasePlayerModel, startPoint : Point, stopPoint : Point, callBack : Function = null, pList : * = null ):void
		{
			player.startMove([stopPoint], callBack, pList );
			if(GameData.SceneType==GameDefine.SceneType_SnowBallFightScene&&MakeSnowLogic.getInstance().isShow)
			{
				//				MakeSnowUI.getInstance().hideCur();
				MakeSnowLogic.getInstance().stopMake();
				SystemSender.getInstance().CustomMsg(CustomToServerFlag.CLIENT_CUSTOMMSG_STOP_MAKEING_SNOWBALL);
			}
		}
		
		//		/**
		//		 *	npc移动 
		//		 * @param npc
		//		 * @param startPoint
		//		 * @param moveRect
		//		 * @param callBack
		//		 * @param pList
		//		 * 
		//		 */
		//		public function npcMove( npc : BasePlayerModel, startPoint : Point, moveRect : Rectangle , callBack : Function = null, pList : * = null ):void
		//		{
		//			npc.startMove( [ _findWayLogic.getWalkNode( randomMovePoint( moveRect ) ) ], callBack, pList );
		//		}
		
		//		private function randomMovePoint( moveRect : Rectangle ):Point
		//		{
		//			var p : Point = new Point();
		//			p.x = int( Math.random() * moveRect.width ) + moveRect.x;
		//			p.y = int( Math.random() * moveRect.height ) + moveRect.y;
		//			
		//			return p;
		//		}
		
		/**
		 * 普通玩家移动
		 */
		private function onPlayerMove(evt : CustomMsgEvent):void
		{
			if(MyPropertyLogic.getInstance().ui.stage) return;
			var pList : Array	=	evt.pList;
			
			if( _model.scene.Role.ObjID.equals( pList[ 0 ] ) )return;
			
			var player : BasePlayerModel = SceneManager.getInstance().getPosMc( pList[ 0 ] );
			
			if(  null != player ) 
			{
				if( (GameData.SceneType == GameDefine.SceneType_WorldBossScene || 
					GameData.SceneType == GameDefine.SceneType_FactionBossScene )&&
					(player.playerObj.GetProperties("WorldPlayerDead")==1 ||
						!_findWayLogic.RoleWalkRect.contains(pList[3],pList[4]))
				)
				{
					return;
				}
				
				if(!player.visible)
				{
					return;
				}
				
				player.moveLogic.speed	=	18;
				player.startMove( [ new Point( pList[3], pList[4] ) ] );
			}
		}
		/**
		 * @param evt 特殊优化 瞬间移动
		 */		
		private function onPlayerStop(evt : CustomMsgEvent):void
		{
			if(GameData.SceneType != GameDefine.SceneType_WorldBossScene && GameData.SceneType != GameDefine.SceneType_FactionBossScene) return;
			var pList : Array	=	evt.pList;
			var player : * = _model.scene.GetObject(pList[0]);
			
			if( _model.scene.Role.ObjID.equals( pList[ 0 ] ) )return;
			
			if(player != null)
			{
				//去场景中找到该玩家的Model
				var playerModel : BasePlayerModel = SceneManager.getInstance().getPosMc(player.ObjID);
				
				if(null == playerModel)
				{
					return;
				}
				
				playerModel.x = pList[1];
				playerModel.y = pList[2];
			}	
		}
		
		public function get findWayLogic():FindWayUtil
		{
			return _findWayLogic;
		}
		
		public function get walkLogic():WalkLogic
		{
			return _walkLogic;
		}
	}
}